// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.TransformManagement;

internal sealed partial class PivotConverter : JsonConverter<Pivot>
{
	public override Pivot Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		var variant = new Pivot();
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				var property = reader.GetString();
				if (property == "aggregations" || property == "aggs")
				{
					variant.Aggregations = JsonSerializer.Deserialize<IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation>?>(ref reader, options);
					continue;
				}

				if (property == "group_by")
				{
					variant.GroupBy = JsonSerializer.Deserialize<IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupBy>?>(ref reader, options);
					continue;
				}
			}
		}

		return variant;
	}

	public override void Write(Utf8JsonWriter writer, Pivot value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.Aggregations is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, value.Aggregations, options);
		}

		if (value.GroupBy is not null)
		{
			writer.WritePropertyName("group_by");
			JsonSerializer.Serialize(writer, value.GroupBy, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(PivotConverter))]
public sealed partial class Pivot
{
	/// <summary>
	/// <para>Defines how to aggregate the grouped data. The following aggregations are currently supported: average, bucket<br/>script, bucket selector, cardinality, filter, geo bounds, geo centroid, geo line, max, median absolute deviation,<br/>min, missing, percentiles, rare terms, scripted metric, stats, sum, terms, top metrics, value count, weighted<br/>average.</para>
	/// </summary>
	public IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.Aggregation>? Aggregations { get; set; }

	/// <summary>
	/// <para>Defines how to group the data. More than one grouping can be defined per pivot. The following groupings are<br/>currently supported: date histogram, geotile grid, histogram, terms.</para>
	/// </summary>
	public IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupBy>? GroupBy { get; set; }
}

public sealed partial class PivotDescriptor<TDocument> : SerializableDescriptor<PivotDescriptor<TDocument>>
{
	internal PivotDescriptor(Action<PivotDescriptor<TDocument>> configure) => configure.Invoke(this);

	public PivotDescriptor() : base()
	{
	}

	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>> AggregationsValue { get; set; }
	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor<TDocument>> GroupByValue { get; set; }

	/// <summary>
	/// <para>Defines how to aggregate the grouped data. The following aggregations are currently supported: average, bucket<br/>script, bucket selector, cardinality, filter, geo bounds, geo centroid, geo line, max, median absolute deviation,<br/>min, missing, percentiles, rare terms, scripted metric, stats, sum, terms, top metrics, value count, weighted<br/>average.</para>
	/// </summary>
	public PivotDescriptor<TDocument> Aggregations(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>>> selector)
	{
		AggregationsValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>>());
		return Self;
	}

	/// <summary>
	/// <para>Defines how to group the data. More than one grouping can be defined per pivot. The following groupings are<br/>currently supported: date histogram, geotile grid, histogram, terms.</para>
	/// </summary>
	public PivotDescriptor<TDocument> GroupBy(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor<TDocument>>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor<TDocument>>> selector)
	{
		GroupByValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor<TDocument>>());
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		if (GroupByValue is not null)
		{
			writer.WritePropertyName("group_by");
			JsonSerializer.Serialize(writer, GroupByValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class PivotDescriptor : SerializableDescriptor<PivotDescriptor>
{
	internal PivotDescriptor(Action<PivotDescriptor> configure) => configure.Invoke(this);

	public PivotDescriptor() : base()
	{
	}

	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor> AggregationsValue { get; set; }
	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor> GroupByValue { get; set; }

	/// <summary>
	/// <para>Defines how to aggregate the grouped data. The following aggregations are currently supported: average, bucket<br/>script, bucket selector, cardinality, filter, geo bounds, geo centroid, geo line, max, median absolute deviation,<br/>min, missing, percentiles, rare terms, scripted metric, stats, sum, terms, top metrics, value count, weighted<br/>average.</para>
	/// </summary>
	public PivotDescriptor Aggregations(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor>> selector)
	{
		AggregationsValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor>());
		return Self;
	}

	/// <summary>
	/// <para>Defines how to group the data. More than one grouping can be defined per pivot. The following groupings are<br/>currently supported: date histogram, geotile grid, histogram, terms.</para>
	/// </summary>
	public PivotDescriptor GroupBy(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor>> selector)
	{
		GroupByValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.TransformManagement.PivotGroupByDescriptor>());
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		if (GroupByValue is not null)
		{
			writer.WritePropertyName("group_by");
			JsonSerializer.Serialize(writer, GroupByValue, options);
		}

		writer.WriteEndObject();
	}
}